package com.chocho.producerserver.dao;

import com.chocho.producerserver.bean.QCollection;
import com.chocho.producerserver.bean.Question;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/11.
 */
@Mapper
@Repository
public interface QuestionDao {
    @Insert("INSERT INTO op_question (question_id, question_num, question_title, question_do_num, question_pass_num, " +
            "question_score, question_context, type_id, qcollection_id, question_difficlut, author, knowledge_node, " +
            "time_limit, memory_limit, question_answer_one, question_answer_two, question_answer_three) " +
            "VALUES(#{questionId}, #{questionNum}, #{questionTitle}, #{questionDoNum}, #{questionPassNum}, " +
            "#{questionScore}, #{questionContext}, #{typeId}, #{qcollectionId}, #{questionDifficult}, #{author}, #{knowledgeNode}, " +
            "#{timeLimit}, #{memoryLimit}, #{questionAnswerOne}, #{questionAnswerTwo}, #{questionAnswerThree})")
    int insert(Question question);

    @Select("<script>" +
            "SELECT * FROM op_question " +
            "<where>" +
            "<trim> " +
            "<if test=\"questionId != \'\' and questionId != null\"> question_id like CONCAT('%',#{questionId},'%')</if> " +
            "</trim> " +
            "<trim prefix='and'> " +
            "<if test=\"questionTitle != \'\' and questionTitle != null\"> question_title like CONCAT('%',#{questionTitle},'%')</if> " +
            "</trim> " +
            "<trim prefix='and'> " +
            "<if test=\"typeId != \'\' and typeId != null\"> type_id like CONCAT('%',#{typeId},'%')</if> " +
            "</trim> " +
            "<trim prefix='and'> " +
            "<if test=\"qcollectionId != \'\' and qcollectionId != null\"> qcollection_id like CONCAT('%',#{qcollectionId},'%')</if> " +
            "</trim> " +
            "<trim prefix='and'> " +
            "<if test=\"questionDifficult != \'\' and questionDifficult != null\"> question_difficlut like CONCAT('%',#{questionDifficult},'%')</if> " +
            "</trim> " +
            "<trim prefix='and'> " +
            "<if test=\"author != \'\' and author != null\"> author like CONCAT('%',#{author},'%')</if> " +
            "</trim> " +
            "<trim prefix='and'> " +
            "<if test=\"knowledgeNode != \'\' and knowledgeNode != null\"> knowledge_node like CONCAT('%',#{knowledgeNode},'%')</if> " +
            "</trim> " +
            "</where>" +
            "</script>")
    List<Question> getQuestionByQuery(Map<String, Object> map);

    @Delete("<script>" +
            "DELETE FROM op_question WHERE question_id in " +
            "<foreach collection=\"list\" open=\"(\" close=\")\" separator=\",\"  item=\"item\"> #{item}" +
            "</foreach>" +
            "</script>")
    int delete(List<String> idList);

    @Update("UPDATE op_question " +
            "SET question_num = #{questionNum}, question_score = #{questionScore}, question_context = #{questionContext}, " +
            "question_title = #{questionTitle}, type_id = #{typeId}, qcollection_id = #{qcollectionId}, question_difficlut = #{questionDifficult}, " +
            "knowledge_node = #{knowledgeNode}, time_limit = #{timeLimit}, memory_limit = #{memoryLimit}, " +
            "question_answer_one = #{questionAnswerOne}, question_answer_two = #{questionAnswerTwo}, question_answer_three = #{questionAnswerThree} " +
            "WHERE question_id = #{questionId}")
    int update(Question question);

    @Select("SELECT COUNT(question_id) FROM op_question")
    int getMaxNum();

    @Select("SELECT SUM(question_score) FROM op_question WHERE qcollection_id = #{id}")
    Integer getAllScoreById(String id);

    @Select("<script>" +
            "SELECT SUM(question_do_num) FROM op_question WHERE qcollection_id in " +
            "<foreach collection=\"list\" open=\"(\" close=\")\" separator=\",\"  item=\"item\"> #{item}" +
            "</foreach>" +
            "</script>")
    List<Integer> selectByIdList(List<String> idList);
}
